package org.boot.ssm.wxshop.web.controller;

import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.domain.*;
import org.boot.ssm.wxshop.service.CommentService;
import org.boot.ssm.wxshop.service.OrderInfoDetailService;
import org.boot.ssm.wxshop.service.OrderInfoService;
import org.boot.ssm.wxshop.service.ReceiveAddressService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @program: wxshop
 * @description: 订单控制器
 * @author: 刘涛
 * @create: 2018-03-15 22:17
 **/
@Controller
@RequestMapping(value = "/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private OrderInfoDetailService orderInfoDetailService;
    @Autowired
    private ReceiveAddressService receiveAddressService;
    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/goOrderListPage")
    /*****************************
     * @Description:去到显示订单列表页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/15
     *******************************/
    public String goOrderListPage() {
        return "backstage/orderInfo/orderlist";
    }

    /*****************************
     * @Description:重定向到前台显示订单列表页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/19
     *******************************/
    @RequestMapping(value = "/frontStage/goLookOrders")
    public String goLookOrders() {
        return "redirect:/orderInfo/frontStage/lookOrders";

    }

    /**
     * 去前台显示订单详情页面
     *
     * @return
     */
    @RequestMapping(value = "/frontStage/goOrderInfoPage")
    public String goOrderInfoPage() {
        return "frontstage/order/order_info";
    }

    /**
     * 到前台显示订单列表页面
     *
     * @return
     */
    @RequestMapping("/frontStage/lookOrders")
    public String lookOrders(@RequestParam(name = "state", required = false) Integer state, Model model) {
        model.addAttribute("state", state);
        return "frontstage/order/look_orders";

    }

    /*****************************
     * @Description: 分页显示订单信息
     * @Param: [pageNum, pageSize, user]
     * @return: java.util.Map<java.lang.String
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               *                                                                                                                                                                                                                                                                ********************/
    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonResult listUser(@RequestParam(value = "pageNum", required = false, defaultValue = "1 ") Integer pageNum, @RequestParam(value = "pageSize ", required = false, defaultValue = "10") Integer pageSize, OrderInfo orderInfo, @RequestParam(name = "firstTime", required = false) String firstTime, @RequestParam(name = "lastTime", required = false) String lastTime) {
        PageInfo<OrderInfo> pageInfo = orderInfoService.selectOrderInfoForPage(pageNum, pageSize, orderInfo, firstTime, lastTime);
        return new JsonResult(0, "成功", pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping(value = "/updateState")
    @ResponseBody
    /*****************************
     * @Description: 修改订单状态
     * @Param: [orderInfo]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/16
     *******************************/
    public JsonResult updateState(OrderInfo orderInfo) {
        return orderInfoService.updateState(orderInfo);
    }

    @RequestMapping(value = "/updateStateMore")
    @ResponseBody
    /*****************************
     * @Description: 批量发货
     * @Param: [list]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/16
     *******************************/
    public JsonResult updateStateMore(@RequestBody List<OrderInfo> list) {
        return orderInfoService.updateStateMore(list);
    }

    /**
     * 显示订单详情
     *
     * @param orderInfoDetail
     * @param model
     * @return
     */
    @RequestMapping(value = "/orderDetil")
    public String orderDetil(OrderInfoDetail orderInfoDetail, Model model) {
        model.addAttribute("list", orderInfoDetailService.queryParamList(orderInfoDetail));
        return "backstage/orderInfo/orderdetil";

    }


    /**
     * 根据用户，订单状态查询所有订单及详情
     *
     * @param state
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontStage/selectOrderInfoAndDetil")
    @ResponseBody
    public JsonResult selectOrderInfoAndDetil(@RequestParam(name = "state", required = false) Integer state, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderInfoService.selectOrderInfoAndDetil(user.getId(), state);
    }

    /**
     * 根据状态以及用户ID查询订单记录条数
     *
     * @param orderInfo
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontStage/countState")
    @ResponseBody
    public JsonResult countState(OrderInfo orderInfo, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderInfoService.countState(orderInfo, user.getId());
    }


    /**
     * 去显示订单详情页面
     *
     * @param orderInfoDetail
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "/frontStage/goOrderDetilPage")
    public String goOrderDetilPage(OrderInfoDetail orderInfoDetail, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("order", orderInfoService.selectOneOrder(orderInfoDetail.getOrderInfoId(), user.getId()));
        model.addAttribute("orderDetils", orderInfoDetailService.queryParamList(orderInfoDetail));
        return "frontstage/order/order_info";
    }

    /**
     * 去支付宝支付页面
     *
     * @param OrderId
     * @param model
     * @return
     */
    @RequestMapping(value = "/frontStage/goPayForZfbPage")
    public String goPayForZfbPage(@RequestParam(name = "orderId", required = true) String orderId, Double sum, Model model) {
        model.addAttribute("orderId", orderId);
        model.addAttribute("sum", sum);
        return "frontstage/order/pay/zfbQr";
    }

    /**
     * 去微信支付页面
     *
     * @param OrderId
     * @param model
     * @return
     */
    @RequestMapping(value = "/frontStage/goPayForWxPage")
    public String goPayForWxPage(@RequestParam(name = "orderId", required = true) String orderId, Double sum, Model model) {
        model.addAttribute("orderId", orderId);
        model.addAttribute("sum", sum);
        return "frontstage/order/pay/wxQr";
    }

    /***
     * 用户支付宝支付
     * @param orderInfo
     * @return
     */
    @RequestMapping("/frontStage/payForZfb")
    @ResponseBody
    public JsonResult payForZfb(OrderInfo orderInfo) {
        orderInfo.setPayMethod(2);
        orderInfo.setState(0);
        return orderInfoService.updateState(orderInfo);
    }

    /***
     * 用户微信支付
     * @param orderInfo
     * @return
     */
    @RequestMapping("/frontStage/payForWx")
    @ResponseBody
    public JsonResult payForWx(OrderInfo orderInfo) {
        orderInfo.setPayMethod(1);
        orderInfo.setState(0);
        return orderInfoService.updateState(orderInfo);
    }

    /**
     * 去付款成功页面
     *
     * @return
     */
    @RequestMapping(value = "/frontStage/goSuccessPage")
    public String goSuccessPage() {
        return "frontstage/order/pay/success";
    }

    /**
     * 去选择收获地址页面
     *
     * @param receiveAddress
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "/frontStage/selectOrderAddress")
    public String selectOrderAddress(ReceiveAddress receiveAddress, HttpSession session, Model model, @RequestParam(name = "orderId") String orderId) {
        User user = (User) session.getAttribute("user");
        receiveAddress.setUserId(user.getId());
        receiveAddress.setIsDelete(1);
        model.addAttribute("orderId", orderId);
        model.addAttribute("address", receiveAddressService.queryParamList(receiveAddress));
        return "frontstage/order/order_address_list";
    }

    /**
     * 修改订单收货地址
     * @param orderInfo
     * @return
     */
    @RequestMapping(value = "/frontStage/updateOrderAddress")
    @ResponseBody
    public JsonResult updateOrderAddress(OrderInfo orderInfo){
        orderInfoService.updateNoNull(orderInfo);
        return new JsonResult(200,"修改成功");

    }

    /**
     * 用户确认收货
     *
     * @param orderInfo
     * @return
     */
    @RequestMapping(value = "/frontStage/confirmReceipt")
    @ResponseBody
    public JsonResult confirmReceipt(OrderInfo orderInfo) {
        orderInfo.setState(2);
        return orderInfoService.updateState(orderInfo);

    }

    /**
     * 去评论页面
     *
     * @param orderId
     * @param model
     * @return
     */
    @RequestMapping(value = "/frontStage/goCommPage")
    public String goCommPage(Integer goodsId, @RequestParam(name = "orderId", required = true) String orderId, Model model) {
        System.out.println(orderId);
        model.addAttribute("goodsId", goodsId);
        model.addAttribute("orderId", orderId);
        return "frontstage/comment/comment";
    }


    /**
     * 添加评论
     *
     * @param comment
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/frontStage/addComment")
    @ResponseBody
    public JsonResult addComment(Comment comment, String orderId, HttpSession httpSession) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setState(3);
        orderInfo.setId(orderId);
        orderInfoService.updateState(orderInfo);
        return commentService.addComment(comment, (User) httpSession.getAttribute("user"));
    }


    /**
     * 逻辑删除订单
     *
     * @param orderInfo
     * @return
     */
    @RequestMapping(value = "/frontStage/deleteOrder")
    @ResponseBody
    public JsonResult deleteOrder(OrderInfo orderInfo) {
        return orderInfoService.deleteOrder(orderInfo);
    }


    /**
     * 立即购买
     *
     * @param goodsId
     * @param specifications
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontStage/payForNow")
    @ResponseBody
    public JsonResult payForNow(Integer goodsId, String specifications, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderInfoService.addOrderForOneGoods(goodsId, specifications, user.getId());


    }



    /**
     * 去结算
     *
     * @param ids
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontStage/addOrderForMoreGoods")
    @ResponseBody
    public JsonResult addOrderForMoreGoods(@RequestBody Integer[] ids, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderInfoService.addOrderForMoreGoods(ids, user.getId());
    }
}